package cn.blb.service.edu.controller;


import cn.blb.common.result.Result;
import cn.blb.service.edu.entity.Teacher;
import cn.blb.service.edu.entity.vo.TeacherQuery;
import cn.blb.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
@Api(tags = "讲师管理")
public class TeacherController {
    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    // 获取所有讲师
    @GetMapping("findAll")
    @ApiOperation(value = "获取所有讲师")
    public Result findAll() {
        List<Teacher> teachers = teacherService.list(null);

        return Result.ok().setData("items", teachers);
    }

    @PostMapping("add")
    @ApiOperation(value = "添加讲师")
    public Result save(@RequestBody
                       @ApiParam(name = "讲师", value = "讲师对象", required = true) Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        return flag ? Result.ok() : Result.error();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询讲师")
    public Result getById(@ApiParam("讲师id") @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return teacher != null ? Result.ok().setData("item", teacher) : Result.error().setMessage("数据不存在");
    }

    @ApiOperation("修改讲师")
    @PostMapping("update")
    public Result updateById(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        return flag ? Result.ok().setMessage("修改成功") : Result.error().setMessage("修改失败");
    }

    @DeleteMapping("delTeacher/{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public Result del(@PathVariable
                      @ApiParam(name = "id", value = "讲师id", required = true) String id) {
        boolean flag = teacherService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }

    // 分页 + 条件查询
    @PostMapping("{current}/{limit}")
    @ApiOperation(value = "分页+条件查询")
    public Result pageList(@ApiParam("当前页") @PathVariable Long current,
                           @ApiParam("每页显示条数") @PathVariable() Long limit,
                           @ApiParam("查询对象") @RequestBody TeacherQuery teacherQuery) {
        // 1.获取page对象
        Page<Teacher> teacherPage = new Page<Teacher>(current, limit);
        // 2.调用查询方法
        IPage<Teacher> pageModel = teacherService.pageQuery(teacherPage, teacherQuery);
        // 3.获取值
        long total = pageModel.getTotal();
        List<Teacher> teachers = pageModel.getRecords();

        return Result.ok().setData("total", total).setData("items", teachers);
    }
}

